dic=dict(one=1,two=2,three=3)
print(dic)
dic=dict((('one',1),('two',2),('three',3)))
print(dic)
dic=dict({'one':1,'two':2,'three':3})
print(dic)
dic=dict([('one',1),('two',2),('three',3)])
print(dic)
d={k:v for k,v in [(1,2),(3,4)]}
print(d)
#验证字典的合法性，字典的键是不可变类型
#dic={[1,2,3]:'alex,1:666'}
# print(dic)TypeError: unhashable type: 'list'
dic={1:'alex',1:'太白',2:'wusir'}#键相同，按最后的取值
print(dic)
dic['age']=23#无则增，有则改
print(dic)
dic.setdefault('hobby','球类运动')#有则不变，无则增加
print(dic)
dic.setdefault('age',10)
print(dic)

#删
res=dic.pop('hobby')
print(res)#返回value
print(dic)
res=dic.pop('hobby','没有此键')#返回值
print(res)
#clear
#dic.clear()
#print(dic){}
del dic['age']
print(dic)

#查
l1=dic.get('hobby_list','没有此键sb')#可以设置返回值
print(l1)

#三个特殊的，keys(),values(),items()
print(dic.keys(),type(dic.keys()))
#可以转换成列表
print(list(dic.keys()))
for key in dic.keys():
    print(key)
print(dic.values())
print(list(dic.values()))


print(dic.items())
for i in dic.items():#(1, '太白')
    print(i)#(2, 'wusir')
for a,b in dic.items():
    print(a,b)
#面试题,一行代码使a,b两个变量的值互换
a=18
b=12
a,b=b,a
print(a,b)

#合并字典
#1.值不想加
d1={'a':1,'b':3}
d2={'b':2}
print({**d1,**d2})

d1={'a':1,'b':3}
d2={'b':2}
print(dict(d1.items()|d2.items()))
d1.update(d2)
print(d1)

lst=[1,3,4,6,7,3,4,2,7,4]
dict1={}
for i in lst:
    dict1.setdefault(i,lst.count(i))
print(dict1)
